package tw.miku.protocol.codec.room;

import java.nio.charset.CharacterCodingException;

import org.apache.mina.core.buffer.IoBuffer;

import tw.miku.constants.ProtocalConstants;
import tw.miku.protocol.codec.AbstractResponseMessagePack;
import tw.miku.util.BufferUtility;

public class AddRoomResponsePack extends AbstractResponseMessagePack
{

	public byte result;

	public int roomId;
	public String roomName;
	public String password;
	public int createUserId;

	public AddRoomResponsePack()
	{
	}

	@Override
	public short getTag()
	{
		return ProtocalConstants.ADD_ROOM;
	}

	@Override
	public IoBuffer encode() throws CharacterCodingException
	{
		IoBuffer buf = IoBuffer.allocate(100).setAutoExpand(true);
		buf.position(4);
		buf.putShort(getTag());
		buf.putShort((short) 0);
		if (result == 0)
		{
			buf.put(result);
			buf.putUnsignedInt(roomId);
			BufferUtility.writeString(buf, roomName);
			BufferUtility.writeString(buf, password);
			buf.putUnsignedInt(createUserId);
		}
		else
		{
			buf.put(result);
		}
		buf.flip();
		buf.putUnsignedInt(buf.limit() - 4);
		buf.position(0);
		return buf;
	}

}
